<!DOCTYPE html>
<html lang="zh-CN">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/favicon.png">
  <link rel="icon" type="image/png" href="/blog/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#35495e">
  <meta name="description" content="小菜头的博客，主要记录学习笔记，包含：html,css,javascript,jQuery,vue等">
  <meta name="author" content="A-cai">
  <meta name="keywords" content="html,css,javascript,jQuery,vue">
  <title>前端传统布局 - A-cai的blog</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/blog/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/atom-one-dark.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">


<link  rel="stylesheet" href="/blog/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/blog/js/utils.js" ></script>
<meta name="generator" content="Hexo 4.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/blog/">&nbsp;<strong>A-cai的学习笔记</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/blog/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2020-06-26 14:04">
      2020年6月26日 下午
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      28
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
              <p class="note note-info">
                
                  本文最后更新于：2020年3月2日 下午
                
              </p>
            
            <article class="markdown-body">
              <p>圣杯布局和双飞翼布局是前端工程师需要日常掌握的重要布局方式。两者的功能相同，都是为了实现一个<strong>两侧宽度固定，中间宽度自适应的三栏布局</strong>。</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-ab11463cd3c26105.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/981/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>圣杯布局与双飞翼布局</p>
<p>圣杯布局来源于文章<a href="https://alistapart.com/article/holygrail" target="_blank" rel="noopener">In Search of the Holy Grail</a>，而双飞翼布局来源于淘宝UED。虽然两者的实现方法略有差异，不过都遵循了以下要点：</p>
<ul>
<li>两侧宽度固定，中间宽度自适应</li>
<li>中间部分在DOM结构上优先，以便先行渲染</li>
<li>允许三列中的任意一列成为最高列</li>
<li>只需要使用一个额外的``标签</li>
</ul>
<p>下面我将依次介绍圣杯布局和双飞翼布局的实现方法，并在最后根据个人思考对原有方法做出一些修改，给出其它一些可行的方案。</p>
<h2 id="圣杯布局"><a href="#圣杯布局" class="headerlink" title="圣杯布局"></a>圣杯布局</h2><h3 id="1-DOM结构"><a href="#1-DOM结构" class="headerlink" title="1. DOM结构"></a>1. DOM结构</h3><div class="hljs"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"header"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"center"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"column"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"left"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"column"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"right"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"column"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"footer"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>

<p>首先定义出整个布局的DOM结构，主体部分是由<code>container</code>包裹的<code>center</code>,<code>left</code>,<code>right</code>三列，其中<code>center</code>定义在最前面。</p>
<h3 id="2-CSS代码"><a href="#2-CSS代码" class="headerlink" title="2. CSS代码"></a>2. CSS代码</h3><p>假设左侧的固定宽度为200px，右侧的固定宽度为150px，则首先在<code>container</code>上设置：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-id">#container</span> &#123;
  <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">200px</span>; 
  <span class="hljs-attribute">padding-right</span>: <span class="hljs-number">150px</span>;
&#125;</code></pre></div>

<p>为左右两列预留出相应的空间，得到如下示意图：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-aaea086d37ac9e06.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/540/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>创建布局框架</p>
<p>随后分别为三列设置宽度与浮动，同时对<code>footer</code>设置清除浮动：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-id">#container</span> <span class="hljs-selector-class">.column</span> &#123;
  <span class="hljs-attribute">float</span>: left;
&#125;

<span class="hljs-selector-id">#center</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
&#125;

<span class="hljs-selector-id">#left</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>; 
&#125;

<span class="hljs-selector-id">#right</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>; 
&#125;

<span class="hljs-selector-id">#footer</span> &#123;
  <span class="hljs-attribute">clear</span>: both;
&#125;</code></pre></div>

<p>得到如下效果：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-908ab18834f384ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/540/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>设置宽度和清除浮动</p>
<p>根据浮动的特性，由于<code>center</code>的宽度为100%，即占据了第一行的所有空间，所以<code>left</code>和<code>right</code>被“挤”到了第二行。</p>
<p>接下来的工作是将<code>left</code>放置到之前预留出的位置上，这里使用<strong><a href="https://www.cnblogs.com/2050/archive/2012/08/13/2636467.html" target="_blank" rel="noopener">负外边距</a>（nagetive margin）</strong>：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-id">#left</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>; 
  <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">100%</span>;
&#125;</code></pre></div>

<p>得到：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-0092cb112431e076.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/540/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>将left移动到预留位置-1</p>
<p>随后还需要使用<strong>定位(position)</strong>方法：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-id">#left</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>; 
  <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">100%</span>;
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">right</span>: <span class="hljs-number">200px</span>;
&#125;</code></pre></div>

<p>这里使用<code>position: relative</code>和<code>right: 200px</code>将<code>left</code>的位置在原有位置基础上左移200px，以完成<code>left</code>的放置：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-0f0134d16c865fb9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/540/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>将left移动到预留位置-2</p>
<p>接下来放置<code>right</code>，只需添加一条声明即可：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-id">#right</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>; 
  <span class="hljs-attribute">margin-right</span>: -<span class="hljs-number">150px</span>; 
&#125;</code></pre></div>

<p>得到最终的效果图：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-e964c6f980b5da16.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/540/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>将right移动到预留位置</p>
<p>至此，布局效果完成。不过还需要考虑最后一步，那就是<strong>页面的最小宽度</strong>：要想保证该布局效果正常显示，由于两侧都具有固定的宽度，所以需要给定页面一个最小的宽度，但这并不只是简单的200+150=350px。回想之前<code>left</code>使用了<code>position: relative</code>，所以就意味着在<code>center</code>开始的区域，还存在着一个<code>left</code>的宽度。所以页面的最小宽度应该设置为200+150+200=550px：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-tag">body</span> &#123;
  <span class="hljs-attribute">min-width</span>: <span class="hljs-number">550px</span>;
&#125;</code></pre></div>

<p>综上所述，圣杯布局的CSS代码为：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-tag">body</span> &#123;
  <span class="hljs-attribute">min-width</span>: <span class="hljs-number">550px</span>;
&#125;

<span class="hljs-selector-id">#container</span> &#123;
  <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">200px</span>; 
  <span class="hljs-attribute">padding-right</span>: <span class="hljs-number">150px</span>;
&#125;

<span class="hljs-selector-id">#container</span> <span class="hljs-selector-class">.column</span> &#123;
  <span class="hljs-attribute">float</span>: left;
&#125;

<span class="hljs-selector-id">#center</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
&#125;

<span class="hljs-selector-id">#left</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>; 
  <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">100%</span>;
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">right</span>: <span class="hljs-number">200px</span>;
&#125;

<span class="hljs-selector-id">#right</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>; 
  <span class="hljs-attribute">margin-right</span>: -<span class="hljs-number">150px</span>; 
&#125;

<span class="hljs-selector-id">#footer</span> &#123;
  <span class="hljs-attribute">clear</span>: both;
&#125;</code></pre></div>

<p>关于圣杯布局的示例，可参考：<a href="https://litaooooo.github.io/page-examples/holy-grail.html" target="_blank" rel="noopener">圣杯布局</a></p>
<p>最后提醒一下很多朋友可能会忽略的小细节：在<code>#center</code>中，包含了一条声明<code>width: 100%</code>，这是中间栏能够做到自适应的关键。可能会有朋友认为不需要设置这条声明，因为觉得<code>center</code>在不设置宽度的情况下会默认将宽度设置为父元素(<code>container</code>)的100%宽度。但需要注意到，<code>center</code>是浮动元素，由于浮动具有<strong>包裹性</strong>，在不显式设置宽度的情况下会自动“收缩”到内容的尺寸大小。如果去掉<code>width: 100%</code>，则当中间栏不包含或者包含较少内容时，整个布局会“崩掉”，而达不到这样的效果：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-09a8a5617072d0a2.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/904/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>中间栏仅包含较少内容</p>
<h2 id="双飞翼布局"><a href="#双飞翼布局" class="headerlink" title="双飞翼布局"></a>双飞翼布局</h2><h3 id="1-DOM结构-1"><a href="#1-DOM结构-1" class="headerlink" title="1. DOM结构"></a>1. DOM结构</h3><div class="hljs"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"header"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"column"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"center"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"left"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"column"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"right"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"column"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"footer"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span></code></pre></div>

<p>双飞翼布局的DOM结构与圣杯布局的区别是用<code>container</code>仅包裹住<code>center</code>，另外将<code>.column</code>类从<code>center</code>移至<code>container</code>上。</p>
<h3 id="2-CSS代码-1"><a href="#2-CSS代码-1" class="headerlink" title="2. CSS代码"></a>2. CSS代码</h3><p>按照与圣杯布局相同的思路，首先设置各列的宽度与浮动，并且为左右两列预留出空间，以及为<code>footer</code>设置浮动清除：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-id">#container</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
&#125;

<span class="hljs-selector-class">.column</span> &#123;
  <span class="hljs-attribute">float</span>: left;
&#125;

<span class="hljs-selector-id">#center</span> &#123;
  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">150px</span>;
&#125;

<span class="hljs-selector-id">#left</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>; 
&#125;

<span class="hljs-selector-id">#right</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>; 
&#125;

<span class="hljs-selector-id">#footer</span> &#123;
  <span class="hljs-attribute">clear</span>: both;
&#125;</code></pre></div>

<p>得到如下效果示意图：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-a0f387a0dc99d388.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1200/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>双飞翼布局初始设置</p>
<p>以上代码将<code>container</code>,<code>left</code>,<code>right</code>设置为<code>float: left</code>，而在<code>container</code>内部，<code>center</code>由于没有设置浮动，所以其宽度默认为<code>container</code>的100%宽度，通过对其设置<code>margin-left</code>和<code>margin-right</code>为左右两列预留出了空间。</p>
<p>将<code>left</code>放置到预留位置：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-id">#left</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>; 
  <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">100%</span>;
&#125;</code></pre></div>

<p>得到：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-7fcff771168c26cb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1200/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>放置left到预留位置</p>
<p>将<code>right</code>放置到预留位置：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-id">#right</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>; 
  <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">150px</span>;
&#125;</code></pre></div>

<p>得到最终效果：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-639e4da232d31e21.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1200/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>双飞翼布局最终效果</p>
<p>最后计算最小页面宽度：由于双飞翼布局没有用到<code>position:relative</code>进行定位，所以最小页面宽度应该为200+150=350px。但是当页面宽度缩小到350px附近时，会挤占中间栏的宽度，使得其内容被右侧栏覆盖，如下所示：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-7612899f07163703.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/442/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>中间栏内容被覆盖</p>
<p>因此在设置最小页面宽度时，应该适当增加一些宽度以供中间栏使用（假设为150px），则有：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-tag">body</span> &#123;
  <span class="hljs-attribute">min-width</span>: <span class="hljs-number">500px</span>;
&#125;</code></pre></div>

<p>至此双飞翼布局大功告成！其布局整体代码为：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-tag">body</span> &#123;
  <span class="hljs-attribute">min-width</span>: <span class="hljs-number">500px</span>;
&#125;

<span class="hljs-selector-id">#container</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
&#125;

<span class="hljs-selector-class">.column</span> &#123;
  <span class="hljs-attribute">float</span>: left;
&#125;
        
<span class="hljs-selector-id">#center</span> &#123;
  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">150px</span>;
&#125;
        
<span class="hljs-selector-id">#left</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>; 
  <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">100%</span>;
&#125;
        
<span class="hljs-selector-id">#right</span> &#123;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>; 
  <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">150px</span>;
&#125;
        
<span class="hljs-selector-id">#footer</span> &#123;
  <span class="hljs-attribute">clear</span>: both;
&#125;</code></pre></div>

<p>关于双飞翼布局的示例，可参考：<a href="https://litaooooo.github.io/page-examples/double-wings.html" target="_blank" rel="noopener">双飞翼布局</a></p>
<h2 id="总结与思考"><a href="#总结与思考" class="headerlink" title="总结与思考"></a>总结与思考</h2><p>通过对圣杯布局和双飞翼布局的介绍可以看出，圣杯布局在DOM结构上显得更加直观和自然，且在日常开发过程中，更容易形成这样的DOM结构（通常<code>和</code>/<code>一起被嵌套在</code>中）；而双飞翼布局在实现上由于不需要使用定位，所以更加简洁，且允许的页面最小宽度通常比圣杯布局更小。</p>
<p>其实通过思考不难发现，两者在代码实现上都额外引入了一个``标签，其目的都是为了既能保证中间栏产生浮动（浮动后还<strong>必须</strong>显式设置宽度），又能限制自身宽度为两侧栏留出空间。</p>
<p>从这个角度出发，如果去掉额外添加的``标签，能否完成相同的布局呢？答案是肯定的，不过这需要在<strong>兼容性上做出牺牲</strong>：</p>
<h3 id="DOM结构"><a href="#DOM结构" class="headerlink" title="DOM结构"></a>DOM结构</h3><div class="hljs"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"header"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"center"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"column"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"left"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"column"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"right"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"column"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"footer"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>

<p>去掉额外的``标签后，得到的DOM结构如上所示，基于双飞翼布局的实现思路，只需要在<code>center</code>上做出修改：</p>
<h3 id="1-使用calc"><a href="#1-使用calc" class="headerlink" title="1. 使用calc()"></a>1. 使用calc()</h3><div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-class">.column</span> &#123;
  <span class="hljs-attribute">float</span>: left;
&#125;
    
<span class="hljs-selector-id">#center</span> &#123;
  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-built_in">calc</span>(<span class="hljs-number">100%</span> - <span class="hljs-number">350px</span>);
&#125;</code></pre></div>

<p>通过<code>calc()</code>可以十分方便地计算出<code>center</code>应该占据的自适应宽度，目前<code>calc()</code>支持到<strong>IE9</strong>。</p>
<h3 id="2-使用border-box"><a href="#2-使用border-box" class="headerlink" title="2. 使用border-box"></a>2. 使用border-box</h3><div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-class">.column</span> &#123;
  <span class="hljs-attribute">float</span>: left;
&#125;
    
<span class="hljs-selector-id">#center</span> &#123;
  <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">padding-right</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
&#125;</code></pre></div>

<p>使用<code>border-box</code>可以将<code>center</code>的整个盒模型宽度设置为父元素的100%宽度，此时再利用<code>padding-left</code>和<code>padding-right</code>可以自动得到中间栏的自适应宽度。不过需要注意的是，由于padding是盒子的一部分，所以padding部分会具有中间栏的背景色，当中间栏高于侧栏时，会出现这样的情况：</p>
<p><img src="https:////upload-images.jianshu.io/upload_images/9397803-243751e1bb945634.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/703/format/webp" srcset="/blog/img/loading.gif" alt="img"></p>
<p>padding背景色影响左侧空间</p>
<p>目前<code>box-sizing</code>支持到<strong>IE8</strong>。</p>
<h3 id="3-使用flex"><a href="#3-使用flex" class="headerlink" title="3.  使用flex"></a>3.  使用flex</h3><p>这里使用flex还是需要与圣杯布局相同的DOM结构，不过在实现上将更加简单：</p>
<div class="hljs"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- DOM结构 --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"center"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"left"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"right"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>

<p>CSS代码如下：</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-id">#container</span> &#123;
    <span class="hljs-attribute">display</span>: flex;
&#125;

<span class="hljs-selector-id">#center</span> &#123;
    <span class="hljs-attribute">flex</span>: <span class="hljs-number">1</span>;
&#125;

<span class="hljs-selector-id">#left</span> &#123;
    <span class="hljs-attribute">flex</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">order</span>: -<span class="hljs-number">1</span>;
&#125;

<span class="hljs-selector-id">#right</span> &#123;
    <span class="hljs-attribute">flex</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">150px</span>;
&#125;</code></pre></div>

<h2 id=""><a href="#" class="headerlink" title=""></a></h2>
            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/blog/2020/06/26/%E4%BB%8B%E7%BB%8D%E9%87%8D%E7%BB%98%E5%92%8C%E5%9B%9E%E6%B5%81/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">介绍重绘和回流</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/blog/2020/06/26/%E5%89%8D%E7%AB%AF%E5%AE%89%E5%85%A8/">
                        <span class="hidden-mobile">前端安全</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
              <!-- Comments -->
              <div class="comments" id="comments">
                
                

              </div>
            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    

    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/debouncer.js" ></script>
<script  src="/blog/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/blog/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/blog/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "前端传统布局&nbsp;",
      ],
      cursorChar: "|",
      typeSpeed: 70,
      loop: true,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/blog/js/local-search.js" ></script>
  <script>
    var path = "/blog/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  













</body>
</html>
